package com.example.recyclerview;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * recycleView使用示例
 */

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    String[] names = {"A", "B", "C",
            "D", "E", "F",
            "G", "H", "I"};
    int[] icon = {R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher};
    //    String[] appNames = {"这是图片A","这是图片B","这是图片A","这是图片A","这是图片A",};
    String appNames = "这是图片";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycler);
        //垂直排列
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        recyclerView.setLayoutManager(linearLayoutManager);
        //九宫格排列
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));

        MyAdapter myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);

    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView name, appName;
            ImageView imageView;

            public MyViewHolder(@NonNull final View itemView) {
                super(itemView);
                name = itemView.findViewById(R.id.name);//没写itemView导致null object
                appName = itemView.findViewById(R.id.app_name);
                imageView = itemView.findViewById(R.id.iv);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "您点击了图片" + name.getText(), Toast.LENGTH_SHORT).show();
                    }
                });
            }


        }

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(MainActivity.this).
                    inflate(R.layout.item, parent, false));
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int position) {
//            myViewHolder.appName.setText(appNames[position]);
            myViewHolder.appName.setText(appNames + (position + 1));
            myViewHolder.name.setText(names[position]);
//            myViewHolder.imageView.setImageResource(icon[position]);
            myViewHolder.imageView.setImageResource(R.mipmap.ic_launcher);
        }

        @Override
        public int getItemCount() {
            return names.length;
        }
    }
}

